package com.example.java_gobang.config;

import com.example.java_gobang.api.GameAPI;
import com.example.java_gobang.api.MatchAPI;
import com.example.java_gobang.api.TestAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

@Configuration
@EnableWebSocket  //让spring框架认识到WebSocket这个类,然后找到TestAPI
public class WebSocketConfig implements WebSocketConfigurer {
    @Autowired
    private TestAPI testAPI;

    @Autowired
    private MatchAPI matchAPI;

    @Autowired
    private GameAPI gameAPI;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {  //能够注册Hander到框架里去
        webSocketHandlerRegistry.addHandler(testAPI,"/test");  //客户端连接路径后就实现 注册实例进去
        webSocketHandlerRegistry.addHandler(matchAPI,"/findMatch")  //看到findMatch就要执行类里面的代码
                .addInterceptors(new HttpSessionHandshakeInterceptor());  //在websocket API的时候,就需要前面准备好的 HttpSession(用户登录保存的) 给搞过来(放到Websocket的Session中)
        webSocketHandlerRegistry.addHandler(gameAPI,"/game") //客户端连接路径后就实现 注册实例进去
        .addInterceptors(new HttpSessionHandshakeInterceptor());  //在websocket API的时候,就需要前面准备好的 HttpSession(用户登录保存的) 给搞过来(放到Websocket的Session中)
    }
}
